﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AvatarHelper;
using Avatar.Model;

namespace Avatar.ResumeService
{
    public static class SelfEvaluationManager
    {
        public static void Save(
            string _user,
            string _evaluation
            )
        {
            #region 入口检测

            if (string.IsNullOrEmpty(_evaluation)) throw new Exception("自我评价内容不能为空");
            if (_evaluation.Length > 250) throw new Exception("自我评价内容最多250个字符");

            #endregion

            try
            {
                using (var _adc = DBHelper.GetDataContext())
                {
                    var _resume = _adc.Resume.Where(r => r.User == _user).FirstOrDefault();

                    if (_resume != null)
                    {
                        if (_resume.SelfEvaluation == null)
                        {
                            _resume.SelfEvaluation = new SelfEvaluation();

                            _resume.SelfEvaluation.ID = Guid.NewGuid();
                            _resume.SelfEvaluation.Evaluation = _evaluation;

                            _adc.SelfEvaluation.InsertOnSubmit(_resume.SelfEvaluation);

                        }
                        else
                        {
                            _resume.SelfEvaluation.Evaluation = _evaluation;
                        }
                    }
                    _resume.UpdateDate = DateTime.Now;
                    _adc.SubmitChanges();
                }
            }
            catch (Exception _e)
            {
                throw new Exception("编辑自我评价出错：" + _e.Message);
            }
        }
    }
}
